<?php

 
 Zend_Loader::loadClass("BaseDBRows");
 Zend_Loader::loadClass("Organization");
 class Organizations extends BaseDBRows
 {
 	protected function setUp()
 	{
 		$this->_tableName = Organization::_tablename;
 	}	
	
	public function getOrganizationSupplier($org_type,$fields=array(),$country_code = ''){
		
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		if(count($fields)) {
			foreach($fields as $key => $value){
				$query->addSelect($value);	
			}
		} else {
			$query->addSelect(" *");
		}
		$query->addFrom($this->_tableName . " a " );
		
		if(strlen($country_code) > 0) {
			Zend_Loader::loadClass("Address");
			$query->addJoin(" LEFT JOIN ". Address::_tablename . " addr on addr.id =a.primary_address_id");
			$query->addWhere(" addr.country_code = '". $country_code . "'");
		}
		
		$query->addWhere("org_type = '". $org_type ."'");
		
		$db = DBCon::instance();
		//echo $query->createSQL();
		$result = $db->executeQuery($query->createSQL());
		return $result;
	}
	
	public function getOrgTypeCountries($orgType) {
	
		Zend_Loader::loadClass("OrganizationAddress");
		Zend_Loader::loadClass("Address");
		Zend_Loader::loadClass("Country");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("distinctrow a.country_code, c.country");
		$query->addFrom($this->_tableName . " org ");
		$query->addJoin(" left join " . OrganizationAddress::_tablename . " oa on org.primary_address_id = oa.org_address_id and org.org_id = oa.org_id ");
		$query->addJoin(" left join " . Address::_tablename . " a on oa.address_id = a.address_id");
		$query->addJoin(", " . Country::_tablename . " c ");
		$query->addWhere("a.country_code = c.country_code");
		$query->addWhere("org.org_type = '" . $orgType . "'");
		$query->addOrderBy("c.country");
		
		$db = DBCon::instance();
		//echo $query->createSQL();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	
	public function getOrgTypeProvinces($orgType) {
		
		Zend_Loader::loadClass("OrganizationAddress");
		Zend_Loader::loadClass("Address");
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect("distinctrow a.province");
		$query->addFrom($this->_tableName . " org ");
		$query->addJoin(" left join " . OrganizationAddress::_tablename . " oa on org.primary_address_id = oa.org_address_id and org.org_id = oa.org_id ");
		$query->addJoin(" left join " . Address::_tablename . " a on oa.address_id = a.address_id");
		$query->addWhere("org.org_type = '" . $orgType . "'");
		$query->addOrderBy("a.province");
		
		$db = DBCon::instance();
		//echo $query->createSQL();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	
	public function getOrganizationPosition($orgType="",$orgId=0, $position=0,$limit=1) {
	
		Zend_Loader::loadClass("QueryCreator");
		
		$query = new QueryCreator();
		$query->addSelect(" * ");
		$query->addFrom($this->_tableName . " o ");
		$query->addOrderBy(" name ");
		
		// orgType
		if (strlen($orgType) > 0) {
			$query->addWhere(" org_type = '" . $orgType . "'");
		}
		
		// orgId
		if (isset($orgId) && $orgId > 0) {
			$query->addWhere(" org_id = " . $orgId);
		}	
		
		// limit: if 0 return all rows
		if(isset($limit) && $limit > 0) {
			$query->addLimit($position . ", " . $limit);		
		}
		
		$db = DBCon::instance();
		echo $query->createSQL();
		$result = $db->executeQuery($query->createSQL());
		return $result;
		
	}
	
 }
?>